package com.geh.learn.jvm;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * Created by gehui on 2017/3/22.
 *
 * VM Args: -XX:MaxMetaspaceSize=10M
 *
 * 在JDK8中, 使用了 MetaSpace 的区域来存放Class的相关信息,
 * 因此当 MetaSpace 内存空间不足时,会抛出 java.lang.OutOfMemoryError: Metaspace 异常.
 *
 *
 */
public class MethodAreaOOMTest {
    public static void main(String[] args) {
        while (true) {
            Enhancer enhancer = new Enhancer();
            enhancer.setSuperclass(MethodAreaOOMTest.class);
            enhancer.setUseCache(false);
            enhancer.setCallback(new MethodInterceptor() {
                public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
                    return methodProxy.invokeSuper(o, objects);
                }
            });

            enhancer.create();
        }
    }
}
